//
//  CategoryDetailsViewController.m
//  ShiWuPai
//
//  Created by app on 16/11/30.
//  Copyright © 2016年 QQ:244987427. All rights reserved.
//

#import "CategoryDetailsViewController.h"
#import "FoodBaseInfoTabViewCell.h"
#import "FoodDetailsViewController.h"
#import "BaseNetworkRequest.h"
#import "BaseNetworkRequest+FoodCategoryDetails.h"

#import "WHFoodDetailsList.h"

#import <SVPullToRefresh.h>

@interface CategoryDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIButton *sortButton;
@property (nonatomic, strong) NSArray *sortKeyWordArray;

@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation CategoryDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //NavigationItem
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_back_dark_20x22_@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"全部" style:UIBarButtonItemStyleDone target:self action:@selector(openMoreKind:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor lightGrayColor]];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    [self requestData];
    // Do any additional setup after loading the view from its nib.
    [self.tableView registerNib:[UINib nibWithNibName:@"FoodBaseInfoTabViewCell" bundle:nil] forCellReuseIdentifier:@"FoodBaseInfoTabViewCell"];
    self.navigationItem.title=self.category.name;
    self.page=1;
    //添加上拉加载
    __weak typeof (CategoryDetailsViewController) *weakSelf=self;
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        weakSelf.page++;
        [weakSelf requestData];
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - event response
-(void)goBack:(UIBarButtonItem *)barButtonItem{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)openMoreKind:(UIBarButtonItem *)barButtonItem{
    
}

#pragma mark - network request
-(void)requestData{
    [BaseNetworkRequest getFoodDetailsListWithOrderAsc:0 Page:self.page Value:self.category.idField Kind:self.kind OrderBy:1 success:^(id responseObj, NSError *error) {
        if (!error) {
            //展示数据
            WHFoodDetailsList *foodDetailsList=responseObj;
            [self.dataArray addObjectsFromArray:foodDetailsList.foods];
            [self.tableView.infiniteScrollingView stopAnimating];
            [self.tableView reloadData];
        }
        
        else{
            //show Error
        }
    }];
}
#pragma mark - UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.dataArray.count;
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WHFood *food=self.dataArray[indexPath.row];
    FoodBaseInfoTabViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"FoodBaseInfoTabViewCell" forIndexPath:indexPath];
    cell.food=food;
    return cell;
}
#pragma mark - UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     WHFood *food=self.dataArray[indexPath.row];
    FoodDetailsViewController *viewController=[[FoodDetailsViewController alloc] init];
    viewController.code=food.code;
    [self.navigationController pushViewController:viewController animated:NO];
    
}

#pragma mark - setter getter
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

-(NSArray *)sortKeyWordArray{
    if (_sortKeyWordArray) {
        _sortKeyWordArray=@[@"热量",@"蛋白质",@"脂肪",@"膳食纤维",@"碳水化合物",@"维生素A",@"维生素B1",@"维生素B2",@"维生素C",@"维生素E",@"烟酸",@"钠",@"钙",@"铁",@"钾",@"碘",@"锌",@"硒",@"镁",@"铜",@"锰",@"胆固醇"];
    }
    return _sortKeyWordArray;
}
@end
